<?php

namespace app\common\service;


use app\common\model\CompanyConfigModel;
use app\common\model\CompanyModel;
use think\cache\driver\Redis;

/**
 * 配置服务
 * Class HttpService
 * @package app\Services\Common\HttpService
 */
class CompanyService
{
    /**
     * 获取公司基本信息
     * @param $company_id
     * @return bool|mixed
     */
    public static function getCompanyInfo($company_id)
    {
        if (empty($company_id)) {
            return false;
        }
        //请求boss后台接口获取入驻商信息
        $redis = new Redis();
        $redis_data = $redis->hget('cloud_company_info_data', $company_id);
//        if (!empty($redis_data)) {
//            return json_decode($redis_data, true);
//        }
//        $url = config('domain_name') . 'Company/getCompany';
//        $company_info = http_post($url, json_encode(['company_id' => $company_id]));
//        if (!empty($company_info) && $company_info['code'] == 1000) {
//            $redis->hset('cloud_company_info_data',$company_id, json_encode($company_info['data']));
//            return $company_info['data'];
//        }
        $companyModel = new CompanyModel();
        $company_info = $companyModel->getOne(['company_id' => $company_id]);
        if (empty($company_info)) {
            return false;
        }
        $company_info['company_seal'] = empty($company_info['company_seal'])?'':config('qi_niu_url').$company_info['company_seal'];
        return $company_info;
    }

    /**
     * 获取公司配置
     * @param $company_id
     * @return bool|mixed
     */
    public static function getCompanyConfig($company_id)
    {
        if (empty($company_id)) {
            return false;
        }
        //请求boss后台接口获取入驻商信息
        $redis = new Redis();
        $redis_data = $redis->hget('cloud_company_config_data', $company_id);
//        if (!empty($redis_data)) {
//            return json_decode($redis_data, true);
//        }
//        $url = config('domain_name') . 'Company/getCompayConfig';
//        $company_info = http_post($url, json_encode(['company_id' => $company_id]));
//        if (!empty($company_info) && $company_info['code'] == 1) {
//            $redis->hset('cloud_company_config_data', $company_id, json_encode($company_info['data']));
//            return $company_info['data'];
//        }
//        return false;
        $companyConfigModel = new CompanyConfigModel();
        $data = $companyConfigModel->getOne(['company_id' => $company_id]);
        if (empty($data)) {
            return false;
        }
        $data['link_config'] = empty($data['link_config']) ? '' : json_decode($data['link_config'], true);
        $img_config = empty($data['img_config']) ? '' : json_decode($data['img_config'], true);
        $data['mp_config'] = empty($data['mp_config']) ? '' : json_decode($data['mp_config'], true);
        $data['wx_config'] = empty($data['wx_config']) ? '' : json_decode($data['wx_config'], true);
        $data['spread_wx_config'] = empty($data['spread_wx_config']) ? '' : json_decode($data['spread_wx_config'], true);
        $data['esign_config'] = empty($data['esign_config']) ? '' : json_decode($data['esign_config'], true);
        $data['sms_config'] = empty($data['sms_config']) ? '' : json_decode($data['sms_config'], true);
        $data['engineer_wx_config'] = empty($data['engineer_wx_config']) ? '' : json_decode($data['engineer_wx_config'], true);
        $data['pay_config'] = empty($data['pay_config']) ? '' : json_decode($data['pay_config'], true);
        $img_config['admin_image'] = empty($img_config['admin_image']) ? '' : uploadImgPath($img_config['admin_image']);
        $img_config['image'] = empty($img_config['image']) ? '' : uploadImgPath($img_config['image']);
        $data['img_config'] = $img_config;
        $redis->hset('cloud_company_config_data', $company_id, json_encode($data));
        return $data;
    }

}
